<template>
  <div class="FootMusic">
    <div class="footerLeft" @click="updatedetailShow">
      <img :src="playList[playListIndex].al.picUrl" alt="" />
      <div>
        <p>{{ playList[playListIndex].name }}</p>
        <span>横滑可以切换生下首噢</span>
      </div>
    </div>
    <div class="footerRight">
      <!-- 播放按钮 -->
      <svg
        class="icon liebiao"
        aria-hidden="true"
        @click="play()"
        v-if="isbtnShow"
      >
        <use xlink:href="#icon-bofanganniu"></use>
      </svg>
      <!-- 暂停按钮 -->
      <svg class="icon liebiao" aria-hidden="true" @click="play()" v-else>
        <use xlink:href="#icon-weibiaoti--"></use>
      </svg>
      <svg class="icon liebiao" aria-hidden="true">
        <use xlink:href="#icon-zu"></use>
      </svg>
    </div>
    <audio
      ref="audio"
      :src="`https://music.163.com/song/media/outer/url?id=${playList[playListIndex].id}.mp3`"
    ></audio>
    <van-popup
      v-model:show="detailShow"
      position="bottom"
      :style="{ height: '100%', width: '100%' }"
    >
      <MusicDetail
        :musicList="playList[playListIndex]"
        :play="play"
        :isbtnShow="isbtnShow"
        :addDuration="addDuration"
      />
    </van-popup>
  </div>
</template>

<script>
import { mapMutations, mapState } from "vuex";
import MusicDetail from "./MusicDetail.vue";
export default {
  data(){
    return{
      interVal:0
    }
  },
  components: {
    MusicDetail,
  },
  computed: {
    ...mapState(["playList", "playListIndex", "isbtnShow", "detailShow"]),
  },
  mounted() {
    console.log(this.$refs);
    // 一开始加载数据则获取歌词
    this.$store.dispatch("getLyric", this.playList[this.playListIndex].id);
  },
  updated() {
    // 数据更改则获取歌词
    this.$store.dispatch("getLyric", this.playList[this.playListIndex].id);
    // 添加歌曲时长
    this.addDuration()
  },
  methods: {
    play() {
      // 判断是暂停还是播放
      if (this.$refs.audio.paused) {
        this.$refs.audio.play();
        this.updateIsbtnShow(false);
        // 播放时开启定时器
        this.updateTime();
      } else {
        this.$refs.audio.pause();
        this.updateIsbtnShow(true);
        // 暂停清除定时器
        clearInterval(this.interVal)
      }
    },
    // 修改实时的时间
    updateTime() {
      // 定时器实时检测播放时间
      this.interVal = setInterval(() => {
        this.updateCurrentTime(this.$refs.audio.currentTime);
      }, 1000);
    },
    // 给歌曲添加总时长
    addDuration(){
      this.updateDuration(this.$refs.audio.duration)
    },
    ...mapMutations([
      "updateIsbtnShow",
      "updatedetailShow",
      "updateCurrentTime",
      "updateDuration"
    ]),
  },
  watch: {
    // 监听播放列表下标如果发生更改则自动播放
    playListIndex() {
      this.$refs.audio.autoplay = true;
      if (this.$refs.audio.paused) {
        // 如果是暂停状态则更改图标
        this.updateIsbtnShow(false);
      }
    },
    // 监听播放列表
    playList() {
      if (this.isbtnShow) {
        this.$refs.audio.autoplay = true;
        this.updateIsbtnShow(false);
      }
    },
  },
};
</script>

<style lang="less" scoped>
.FootMusic {
  width: 100%;
  height: 1.4rem;
  position: fixed;
  bottom: 0;
  border-top: 1px solid #999;
  background-color: #fff;
  display: flex;
  padding: 0.2rem;
  justify-content: space-between;
  .footerLeft {
    width: 60%;
    height: 100%;
    display: flex;
    justify-content: space-around;
    align-items: center;
    img {
      width: 1rem;
      height: 1rem;
      border-radius: 50%;
    }
  }
  .footerRight {
    width: 20%;
    height: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    .icon {
      width: 0.6rem;
      height: 0.6rem;
    }
  }
}
</style>
   